package com.amigo.online.provider.manager.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.provider.manager.model.entity.GlobalConfig;
import com.amigo.online.provider.manager.service.GlobalConfigService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@RestController
@RequestMapping(value = "/globalconfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class GlobalConfigController extends BaseController {

    @Resource
    private GlobalConfigService globalConfigService;


    @PostMapping(value = "/getglobalConfigList")
    public ResponseEntity<Response> getglobalConfigList(@RequestBody GlobalConfig globalConfig) {
        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(globalConfig.getPageNo(), 10);
        List<GlobalConfig> globalConfiglist = globalConfigService.select(globalConfig);
        result.put("globalConfiglist", globalConfiglist);
        result.put("pagetotal", page.getTotal());
        return success(result);

    }

    /**
     * 添加全局设置
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/insertionGlobalConfig")
    public ResponseEntity<Response> insertionGlobalConfig(@RequestBody GlobalConfig globalConfig) {
        if (globalConfigService.save(globalConfig) <= 0) {
            return success(false);
        } else {
            return success(true);
        }


    }

    /**
     * 修改全局设置
     *
     * @param dto
     * @return
     */
    @PutMapping(value = "/updateGlobalConfig")
    public ResponseEntity<Response> updateGlobalConfig(@RequestBody GlobalConfig globalConfig) {
        if (globalConfigService.update(globalConfig) <= 0) {
            return success(false);
        } else {
            return success(true);
        }
    }

    /**
     * 删除全局设置
     *
     * @param dto
     * @return
     */
    @DeleteMapping(value = "/deleteGlobalConfig")
    public ResponseEntity<Response> deleteGlobalConfig(@RequestBody GlobalConfig globalConfig) {
        if (globalConfigService.delete(globalConfig) <= 0) {
            return success(false);
        } else {
            return success(true);
        }
    }


}
